package cn.shop.alibaba.service.impl;

import cn.shop.alibaba.beans.Order;
import cn.shop.alibaba.beans.OrderItem;
import cn.shop.alibaba.beans.Product;
import cn.shop.alibaba.beans.User;
import cn.shop.alibaba.constants.HttpCode;
import cn.shop.alibaba.dto.OderParams;
import cn.shop.alibaba.feign.ProductServiceFeign;
import cn.shop.alibaba.feign.UserServiceFeign;
import cn.shop.alibaba.mapper.OrderItemMapper;
import cn.shop.alibaba.mapper.OrderMapper;
import cn.shop.alibaba.resp.Result;
import cn.shop.alibaba.service.OrderService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

@Slf4j
@Service(value = "orderServiceV4")
public class OrderServiceV4Impl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private UserServiceFeign userServiceFeign;

    @Autowired
    private ProductServiceFeign productServiceFeign;


    //提交订单成功后，发送给用户订单消息
    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrder(OderParams oderParams) {
//        if (ObjectUtils.isEmpty(oderParams)){
//            throw new RuntimeException("参数异常: " + JSONObject.toJSONString(oderParams));
//        }

        User user =  userServiceFeign.getuser(oderParams.getId());
        if (user == null) {
            throw new RuntimeException("未获取到用户信息: " + JSONObject.toJSONString(oderParams));
        }
        if (user.getId() == -1){
            throw new RuntimeException("触发了用户微服务的容错逻辑: " + JSONObject.toJSONString(oderParams));
        }
        Product product = productServiceFeign.getProductById(oderParams.getProId());
        if (product == null){
            throw new RuntimeException("未获取到商品信息: " + JSONObject.toJSONString(oderParams));
        }
        if (product.getId() == -1){
            throw new RuntimeException("触发了商品微服务的容错逻辑: " + JSONObject.toJSONString(oderParams));
        }
        if (product.getProStock() < oderParams.getNumber()){
            throw new RuntimeException("商品库存不足: " + JSONObject.toJSONString(oderParams));
        }
        Order order = new Order();
        order.setAddress(user.getAddress());
        order.setPhone(user.getPhone());
        order.setUserId(user.getId());
        order.setUsername(user.getUsername());
        order.setTotalPrice(product.getProPrice().multiply(BigDecimal.valueOf(oderParams.getNumber())));
        orderMapper.insert(order);

        OrderItem orderItem = new OrderItem();
        orderItem.setNumber(oderParams.getNumber());
        orderItem.setOrderId(order.getId());
        orderItem.setProId(product.getId());
        orderItem.setProName(product.getProName());
        orderItem.setProPrice(product.getProPrice());
        orderItemMapper.insert(orderItem);

        Result<Integer> result = productServiceFeign.update_count(oderParams.getProId(),oderParams.getNumber());
        if (result.getCode() == 1001){
            throw new RuntimeException("触发了商品微服务的容错逻辑: " + JSONObject.toJSONString(oderParams));
        }
        if (result.getCode() != HttpCode.SUCCESS){
            throw new RuntimeException("库存扣减失败");
        }
        log.info("库存扣减成功");

        //发送用户订单消息
        rocketMQTemplate.convertAndSend("order-topic", order);


    }
}
